<?php


namespace App\Http\Controllers\Admin\Game;


use  App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use App\Response\Response;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class GameConfigLogController extends BaseController
{

    //获取最新的配置纪录
    /**
     * @api {get} /guessing/config/log 获取最新的配置纪录
     * @apiVersion 0.1.0
     * @apiName 获取最新的配置纪录
     * @apiGroup 霍克城堡 竞猜
     * @apiParam {Int}                     page  （选填）
     * @apiParam {Int}                     page_size  （选填）
     * @apiParam {Int}                     start_time 开始时间 秒级时间戳（选填）
     * @apiParam {Int}                     end_time 结束时间（选填）
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": {
     * "total": 8,
     * "current_page": 1,
     * "last_page": 8,
     * "list": [
     * {
     * "round_id": 20210809163400,
     * "odds": 12321,//预计产出比 1：odds/100
     * "output_limit": 123,
     * "history_output_limit": 12321,
     * "history_odds": 23,//实际产出比 1:history_odds/100
     * "history_input": 18293, //本场总投入  实时盈亏=history_input-history_output
     * "history_output": 4179,//总体最大亏损
     * "notice_count": 9000,
     * "max_output": 2700,//单场最大亏损
     * "is_open": 1,
     * "slots": "[{\"index\": 1, \"times\": \"11\"}, {\"index\": 2, \"times\": \"6\"}, {\"index\": 3, \"times\": \"3\"}, {\"index\": 4, \"times\": \"4\"}, {\"index\": 5, \"times\": \"5\"}, {\"index\": 6, \"times\": \"61\"}, {\"index\": 7, \"times\": \"89\"}, {\"index\": 8, \"times\": \"89\"}]",
     * "created_at": "2021-08-09 16:34:00.021102",
     * "updated_at": "2021-08-09 16:55:20.080035"
     * }
     * ]
     * }
     * }
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function logList(Request $request)
    {
        $data = $request->only(['page', 'page_size', 'start_time', 'end_time']);

        $va = app('validator')->make($data, [
            'page' => 'integer|min:1',//
            'page_size' => 'integer|min:1',//
            'start_time' => 'integer|min:1',//开始时间
            'end_time' => 'integer|min:1',//结束时间
        ]);
        if ($va->fails()) {
            throw  new BasicException(10001, $va->errors()->first());

        }
        $data['page'] = $data['page'] ?? 1;
        $data['page_size'] = $data['page_size'] ?? 20;

        if (isset($data['start_time'])) {
            $where[] = ['created_at', '>', date('Y-m-d H:i:s', $data['start_time'])];
        }
        if (isset($data['end_time'])) {
            $where[] = ['created_at', '<', date('Y-m-d H:i:s', $data['end_time'])];
        }
        if (isset($where)) {
            $list = app('model_game_config_log')->orderBy('created_at', 'desc')
                ->where($where)
                ->paginate($data['page_size'], ['*'], '', $data['page'])->toArray();
        } else {
            $list = app('model_game_config_log')->orderBy('created_at', 'desc')
                ->paginate($data['page_size'], ['*'], '', $data['page'])->toArray();
        }
        //返回值有固定格式
        $result = [
            'total' => $list['total'] ?? 0,
            'current_page' => $list['current_page'] ?? 1,
            'last_page' => $list['last_page'] ?? 1,
            'list' => $list['data']
        ];
        return Response::sendData($result);

    }


    /**
     * @api {get} /guessing/config/log/round 开奖纪录
     * @apiVersion 0.1.0
     * @apiName 开奖纪录
     * @apiGroup 霍克城堡 竞猜
     * @apiParam {Int}                     page  （选填）
     * @apiParam {Int}                     page_size  （选填）
     * @apiParam {Int}                     start_time 开始时间 秒级时间戳（选填）
     * @apiParam {Int}                     end_time 结束时间（选填）
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": {
     * "total": 516,
     * "current_page": 1,
     * "last_page": 258,
     * "list": [
     * {
     * "id": 20210730185400,//序号
     * "hit_slot": 2,//命中卡槽
     * "input": 1416159,//本轮投入
     * "output": 329692,// 本场支出
     * "odds": 100,//
     * "abs_odds": 86,
     * "history_odds": 14,
     * "is_reward": 1,
     * "is_visible": 1,
     * "snapshot": "{\"odds\": 100, \"slot\": {\"index\": 2, \"output\": 329692, \"abs_odds\": 86, \"raw_odds\": 14, \"error_odds\": 6.149797636673596}, \"input\": 1416159, \"slots\": {\"1\": {\"index\": 1, \"output\": 175798, \"abs_odds\": 86, \"raw_odds\": 14, \"error_odds\": 6.189244942420342}, \"2\": {\"index\": 2, \"output\": 329692, \"abs_odds\": 86, \"raw_odds\": 14, \"error_odds\": 6.149797636673596}, \"3\": {\"index\": 3, \"output\": 500985, \"abs_odds\": 85, \"raw_odds\": 15, \"error_odds\": 6.106396467747908}, \"4\": {\"index\": 4, \"output\": 712452, \"abs_odds\": 85, \"raw_odds\": 15, \"error_odds\": 6.053537624801098}, \"5\": {\"index\": 5, \"output\": 871345, \"abs_odds\": 85, \"raw_odds\": 15, \"error_odds\": 6.014334844033476}, \"6\": {\"index\": 6, \"output\": 1138206, \"abs_odds\": 85, \"raw_odds\": 15, \"error_odds\": 5.949465185119058}, \"7\": {\"index\": 7, \"output\": 1309749, \"abs_odds\": 85, \"raw_odds\": 15, \"error_odds\": 5.908395609831973}, \"8\": {\"index\": 8, \"output\": 1255310, \"abs_odds\": 85, \"raw_odds\": 15, \"error_odds\": 5.921376319276605}}, \"inputs\": {\"1\": 175798, \"2\": 164846, \"3\": 166995, \"4\": 178113, \"5\": 174269, \"6\": 189701, \"7\": 187107, \"8\": 179330}, \"output\": 329692, \"slots_times\": {\"1\": 1, \"2\": 2, \"3\": 3, \"4\": 4, \"5\": 5, \"6\": 6, \"7\": 7, \"8\": 7}, \"history_odds\": 14, \"output_limit\": 111, \"history_input\": 200530932, \"history_output\": 28047078, \"history_output_limit\": 111}",
     * "created_at": "2021-07-30 18:54:00.011526",
     * "updated_at": "2021-07-30 18:54:02.217688",
     * "round_stat": {
     * "round_id": 20210730185400,
     * "all_count": 2595,//本轮全部参与人数
     * "hit_count": 313,//盈利人数
     * "miss_count": 2282,//亏损人数
     * "max_output": 1992,//最大亏损
     * "created_at": "2021-07-30 18:54:00.059671",
     * "updated_at": "2021-07-30 18:54:00.060882"
     * }
     * },
     * {
     * "id": 20210730185320,
     * "hit_slot": 1,
     * "input": 1736624,
     * "output": 231676,
     * "odds": 100,
     * "abs_odds": 86,
     * "history_odds": 14,
     * "is_reward": 1,
     * "is_visible": 1,
     * "snapshot": "{\"odds\": 100, \"slot\": {\"index\": 1, \"output\": 231676, \"abs_odds\": 86, \"raw_odds\": 14, \"error_odds\": 6.183750047713735}, \"input\": 1736624, \"slots\": {\"1\": {\"index\": 1, \"output\": 231676, \"abs_odds\": 86, \"raw_odds\": 14, \"error_odds\": 6.183750047713735}, \"2\": {\"index\": 2, \"output\": 392332, \"abs_odds\": 85, \"raw_odds\": 15, \"error_odds\": 6.1423514248238815}, \"3\": {\"index\": 3, \"output\": 643215, \"abs_odds\": 85, \"raw_odds\": 15, \"error_odds\": 6.078648508608132}, \"4\": {\"index\": 4, \"output\": 851712, \"abs_odds\": 85, \"raw_odds\": 15, \"error_odds\": 6.026566248686984}, \"5\": {\"index\": 5, \"output\": 1060265, \"abs_odds\": 85, \"raw_odds\": 15, \"error_odds\": 5.975231114018701}, \"6\": {\"index\": 6, \"output\": 1365114, \"abs_odds\": 85, \"raw_odds\": 15, \"error_odds\": 5.901528115800089}, \"7\": {\"index\": 7, \"output\": 1503215, \"abs_odds\": 85, \"raw_odds\": 15, \"error_odds\": 5.868649768834132}, \"8\": {\"index\": 8, \"output\": 1589924, \"abs_odds\": 85, \"raw_odds\": 15, \"error_odds\": 5.848166165525401}}, \"inputs\": {\"1\": 231676, \"2\": 196166, \"3\": 214405, \"4\": 212928, \"5\": 212053, \"6\": 227519, \"7\": 214745, \"8\": 227132}, \"output\": 231676, \"slots_times\": {\"1\": 1, \"2\": 2, \"3\": 3, \"4\": 4, \"5\": 5, \"6\": 6, \"7\": 7, \"8\": 7}, \"history_odds\": 14, \"output_limit\": 111, \"history_input\": 199114773, \"history_output\": 27717386, \"history_output_limit\": 111}",
     * "created_at": "2021-07-30 18:53:20.072421",
     * "updated_at": "2021-07-30 18:53:22.738567",
     * "round_stat": {
     * "round_id": 20210730185320,
     * "all_count": 3149,
     * "hit_count": 415,
     * "miss_count": 2734,
     * "max_output": 998,
     * "created_at": "2021-07-30 18:53:20.124736",
     * "updated_at": "2021-07-30 18:53:20.125739"
     * }
     * }
     * ]
     * }
     * }
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function roundList(Request $request)
    {

        $data = $request->only(['page', 'page_size', 'start_time', 'end_time']);

        $va = app('validator')->make($data, [
            'page' => 'integer|min:1',//
            'page_size' => 'integer|min:1',//
            'start_time' => 'integer|min:1',//开始时间
            'end_time' => 'integer|min:1',//结束时间
        ]);
        if ($va->fails()) {
            throw  new BasicException(10001, $va->errors()->first());
        }
        $data['page'] = $data['page'] ?? 1;
        $data['page_size'] = $data['page_size'] ?? 20;
        if (isset($data['start_time'])) {
            $where[] = ['created_at', '>', date('Y-m-d H:i:s', $data['start_time'])];
        }
        if (isset($data['end_time'])) {
            $where[] = ['created_at', '<', date('Y-m-d H:i:s', $data['end_time'])];
        }
        if (isset($where)) {
            $list = app('model_game_round')->orderBy('id', 'desc')
                ->where($where)
                ->with(['roundStat' => function ($query) {
                    $query->select(['*']);
                }])
                ->paginate($data['page_size'], ['*'], '', $data['page'])->toArray();
        } else {
            $list = app('model_game_round')->orderBy('id', 'desc')
                ->with(['roundStat' => function ($query) {
                    $query->select(['*']);
                }])
                ->paginate($data['page_size'], ['*'], '', $data['page'])->toArray();
        }
        //返回值有固定格式
        $result = [
            'total' => $list['total'] ?? 0,
            'current_page' => $list['current_page'] ?? 1,
            'last_page' => $list['last_page'] ?? 1,
            'list' => $list['data']
        ];
        return Response::sendData($result);
    }


    /**
     * @api {get} /guessing/config/log/user 开奖详情
     * @apiVersion 0.1.0
     * @apiName 获取最新的配置纪录
     * @apiGroup 霍克城堡 竞猜
     * @apiParam {Int}                     page 产出限制 （选填）
     * @apiParam {Int}                     page_size 历史产出限制 （选填）
     * @apiParam {Int}                     round_id 开奖id （选填）
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": {

     * "total": 1,
     * "current_page": 1,
     * "last_page": 1,
     * "list": [
     * {
     * "id": 1309957,
     * "nickname": "伯涵菡8363233",
     * "bet": [//投注情况 slot-input  本次投入=这个用户下bet对象所有的input和   本次产出=这个用户下bet对象所有is_hit==1的output和
     * {
     * "user_id": 1309957,
     * "round_id": 20210819135200,
     * "slot": 7,
     * "input": 1,
     * "output": 4,
     * "is_hit": 0
     * },
     * {
     * "user_id": 1309957,
     * "round_id": 20210819135200,
     * "slot": 3,
     * "input": 1,
     * "output": 10,
     * "is_hit": 0
     * },
     * {
     * "user_id": 1309957,
     * "round_id": 20210819135200,
     * "slot": 4,
     * "input": 2,
     * "output": 14,
     * "is_hit": 1
     * },
     * {
     * "user_id": 1309957,
     * "round_id": 20210819135200,
     * "slot": 5,
     * "input": 1,
     * "output": 7,
     * "is_hit": 0
     * }
     * ]
     * }
     * ]
     * }
     * }
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function userRound(Request $request)
    {

        $data = $request->only(['round_id', 'page', 'page_size'], null);
        $va = app('validator')->make($data, [
            'page' => 'integer|min:1',//
            'page_size' => 'integer|min:1',//
            'round_id' => 'required|integer|min:1',
        ]);
        if ($va->fails()) {
            throw  new BasicException(10001, $va->errors()->first());
        }
        $data['page'] = $data['page'] ?? 1;
        $data['page_size'] = $data['page_size'] ?? 20;
        $where[] = ['round_id', '=', $data['round_id']];

        $list = app('model_game_user')
            ->whereHas('bet', function ($query) use ($where) {
                $query->where($where);
            })
            ->with(['bet' => function ($query) {
                $query->select(['user_id', 'round_id', 'slot', 'input', 'output', 'is_hit']);

            }])
            ->paginate($data['page_size'], ['id', 'nickname'], '', $data['page'])
            ->toArray();

        $resData = [];
        foreach ($list['data'] as $k => $v) {
            $resData[$k] = $v;
            foreach ($v['bet'] as $kk => $vk) {
                if ($vk['round_id'] != $data['round_id']) {
                    unset($resData[$k]['bet'][$kk]);
                }
            }
            $resData[$k]['bet'] = array_values($resData[$k]['bet']);

        }
        $result = [
            'total' => $list['total'] ?? 0,
            'current_page' => $list['current_page'] ?? 1,
            'last_page' => $list['last_page'] ?? 1,
            'list' => $resData
        ];
        return Response::sendData($result);
    }


}